<?php

namespace app\bews\controller;

use app\bews\model\Base;
use think\app;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Zan extends Base
{
    public function zanInfo(){
        $lists = Db::table("bew_zan")->select();
        View::assign([
            'lists'=>$lists,
        ]);
        return View::fetch();
    }

    public function zanAdd(){
        if(Request::isPost()){
//            print_r(input('post.'));
            $name = input('post.name');
            $type = input('post.type');
            $desc1 = input('post.desc1');
            $desc2 = input('post.desc2');
            $thumb = input('post.thumb');

            $insert = Db::table('bew_zan')->insert([
                'name'=>$name,
                'type'=>$type,
                'desc1'=>$desc1,
                'desc2'=>$desc2,
                'thumb'=>$thumb
            ]);
            if(empty($insert)){
                echo json_encode(['code'=>1,'msg'=>'添加失败,请重试']);
                exit();
            }else{
                echo json_encode(['code'=>0,'msg'=>'添加成功']);
            }
        }else{
            return View::fetch();
        }
    }

    public function zanModify(){
        if(Request::isPost()){
            $id = input('post.id');
            $name = input('post.name');
            $type = input('post.type');
            $desc1 = input('post.desc1');
            $desc2 = input('post.desc2');
            $thumb = input('post.thumb');

            $update = Db::table("bew_zan")->where('id',$id)->update([
                'name'=>$name,
                'type'=>$type,
                'desc1'=>$desc1,
                'desc2'=>$desc2,
                'thumb'=>$thumb
            ]);
            if($update == null){
                echo json_encode(['code'=>1,'msg'=>'修改失败,请重试']);
                exit();
            }
            echo json_encode(['code'=>0,'msg'=>'修改成功']);
        }else{
            $id = input('get.id');
            if(empty($id)){
                $this->error("请传入id");
                exit();
            }
            $find = Db::table("bew_zan")->where('id',$id)->find();
            View::assign([
                'find'=>$find
            ]);
            return View::fetch();
        }
    }

    public function zanDel(){
        $id = input('post.id');
        if(empty($id)){
            $this->error("请传入id");
            exit();
        }
        $del = Db::table("bew_zan")->where('id',$id)->delete();
        if($del == null){
            echo json_encode(['code'=>1,'msg'=>'删除失败,请重试']);
            exit();
        }
        echo json_encode(['code'=>0,'msg'=>'删除成功']);
        return View::fetch();
    }
}